//注册页面
var imagePath = '';
var imageBase64 = '';
Page({
  data: {
    photoStatus: true,
    saveImagePath: '',
    //用户人脸Base64对象
    userimgebaser: {
      imageBase: ''
    },
    //人脸token
    face_token: '',
    //注册传给后端的对象定义
    ot: {
      openid: '',
      token: ''
    },
    display_title:true,//显示提示信息
    dusplay_content:true,//显示主题信息
    start:true,//开始答题按钮
    failure:true//失败按钮
  },

  //启动相机拍照
  camera: function () {
    var that = this
    var imageBase = "userimgebaser.imageBase"
    this.ctx.takePhoto({
      quality: 'low',
      success: function (res) {
        wx.showLoading({
          title: '添加人脸数据中'
        })
        // 获取临时路径
        imagePath = res.tempImagePath,
          //console.log(imagePath)
          //显示数据
          that.setData({
            photoStatus: false,
            canback: res.tempImagePath
          })
        wx.getFileSystemManager().readFile({
          filePath: imagePath, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            //赋值
            that.setData({
              [imageBase]: res.data,
            }),
              //图片64
              //console.log(res.data);
            wx.showLoading({
              title: '上传中..'
            })
            var json = {};
            json.imageBase = res.data;
            //图盘base console.log(json);
            console.log("===============",json)
            wx.request({
              url: 'http://10.20.58.235:8080/v1/open/user/manager/faceRe',
              method: 'POST',
              data: that.data.userimgebaser,
              success: function (e) {
                console.log(e)
                that.setData({
                  face_token: e.data
                })
                wx.hideLoading()
                wx.showToast({
                  title: '上传成功',
                  icon: 'success',
                  duration: 1000
                })
              }, fail: function (e) {
                wx.showToast({
                  title: '上传失败',
                  icon: 'success',
                  image: 'none',
                  duration: 1000
                })
              }
            })
          }
        })
        wx.hideLoading()
      }
    })
  },
  //点击前去登录(注册token)
  oklogo: function () {
    var that = this;
    var token = "ot.token";
    var openid = "ot.openid"
    wx.getStorage({
      key: 'openid',
      success(res) {
        console.log(res)
        that.setData({
          [openid]: res.data,
          [token]: that.data.face_token
        })
        console.log("id"+that.data.ot.openid);
        console.log("gace"+that.data.face_token);
        wx.request({
          url: 'http://10.20.58.235:8080/v1/open/user/manager/setToken',
          method: 'POST',
          data: that.data.ot,
          header: {
            "Content-Type": "application/json"
          },
          success: function (e) {
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 1000,
            })
            console.log("更新数据成功");
              that.setData({
                display_title:false,
                dusplay_content:false,
                start:false
              }),
                wx.switchTab({
                  url: '../starting_to_work/starting_to_work',
                })
          },fail:function(e){
            wx.showToast({
              title: '注册失败',
              icon: 'none',
              duration: 1000
            })
            that.setData({
              failure:false
            })
            console.log("=======================")
          }
        })
      }
    })
  },
  //点击开始答题
  start:function(res){
    wx.navigateTo({
      url: '../starting_to_work/starting_to_work',
    })
  },
  onLoad: function (options) {
    this.ctx = wx.createCameraContext()
  }
})